import { Button } from 'antd';
import React, { useEffect, useRef ,useState} from 'react';
import { useNavigate } from 'react-router-dom';
import { useRequest } from 'ahooks';
import { ipcRenderer, ipcMain } from 'electron';
import { registerBridge, sendMessage } from '@/renderer/pages/file/message';
import { Avatar, Space } from 'antd';
import {
  MinusOutlined,
  CloseOutlined,
  BorderOutlined,
} from '@ant-design/icons';
import './App.css';
import CloseApp from './components/closeApp';

export default function Home() {
  const iframeRef = useRef(null);
  const baseUrl = window.electron.store.get('baseUrl');
  const iframeUrl = baseUrl + 'electron?common=true';
  const [closeVisible, setCloseVisible] = useState<boolean>(false);

  const doOperton = (type) => {
    window.electron.ipcRenderer.sendMessage('index:opertion', type);
  };

    // 关闭
    window.electron.ipcRenderer.on('win-close-tips', () => {
      setCloseVisible(true);
      console.log('关闭弹窗');
    });
  

  // useEffect(() => {
  //   ipcMain.on('window-resize', () => {
  //     const { innerWidth, innerHeight } = window;

  //     iframeRef.current.style.width = innerWidth;
  //     iframeRef.current.style.height = innerHeight;
  //   });

  //   return () => {
  //     ipcMain.removeAllListeners('window-resize');
  //   };
  // }, []);

  useEffect(() => {
    // 监听来自 iframe 页面的消息
    registerBridge((data: any, key: string) => {
      switch (key) {
        case 'renderFinish':
          // 组件初始化后跳转登录
          break;
        case 'login':
          window.electron.store.set('user', { ...data });
          window.electron.ipcRenderer.sendMessage('login:closewindow');
          window.electron.ipcRenderer.sendMessage('index:openwindow');

          break;

        default:
          break;
      }
    });
    return () => {
      window.removeEventListener('message', () => {});
    };
  }, []);

  return (
    <div   style={{
      position: 'relative',
    }}>
       <div
          style={{
            position: 'absolute',
            right: 10,
            fontSize: 12,
            top: -20,
          }}
        >
          <Space size={20}>
            <MinusOutlined
              onClick={(e) => {
                e.preventDefault();
                doOperton('hide');
              }}
              style={{ cursor: 'pointer' }}
            />
{/* 
            <BorderOutlined
              style={{ cursor: 'pointer' }}
              onClick={(e) => {
                e.preventDefault();

                doOperton('full');
              }}
            /> */}
            <CloseOutlined
              style={{ cursor: 'pointer' }}
              onClick={(e) => {
                e.preventDefault();
                doOperton('quit2');
              }}
            />
          </Space>
        </div>
      <div style={{ padding: '15px 0' }}>
        <iframe
          width="100%"
          height="500"
          frameBorder="0"
          style={{ border: 'none' }}
          ref={iframeRef}
          src={iframeUrl}
        ></iframe>
      </div>
      
      <CloseApp
        visible={closeVisible}
        setVisible={(reload: boolean) => {
          if (reload) {
            // window.electron.ipcRenderer.sendMessage('path:save', folderPath);
          }
          setCloseVisible(false);
        }}
      />
    </div>
  );
}
